import VueRouter from 'vue-router'
import Vue from 'vue'
import MainLayout from '@/layout/Main'
import Dashboard from '@/views/Dashboard'
import Site from '@/views/site/Site'
import Ftp from '@/views/ftp/Ftp'
import Database from '@/views/database/Database'
import Soft from '@/views/soft/Soft'
import Safe from '@/views/safe/Safe'
import File from '@/views/file/File'
import Setting from '@/views/setting/Setting'
import Crontab from '@/views/crontab/Crontab'
import Login from '@/views/login/Login'
import Member from '@/views/member'
import NotFound from '@/views/notfound'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'home',
        component: MainLayout,
        meta: {
            title: '首页'
        },
        children: [
            {
                path: '/member',
                name: 'member',
                component: Member,
                meta: {
                    title: '会员管理'
                }
            },
            {
                path: '/dashboard',
                name: 'dashboard',
                component: Dashboard,
                meta: {
                    title: '仪表盘'
                }
            },
            {
                path: '/site',
                name: 'site',
                component: Site,
                meta: { title: '网站' }
            },
            {
                path: 'ftp',
                name: 'ftp',
                component: Ftp,
                meta: {
                    title: '监控'
                }
            },
            {
                path: '/safe',
                name: 'safe',
                component: Safe,
                meta: {
                    title: '安全'
                }
            },
            {
                path: 'database',
                name: 'database',
                component: Database,
                meta: {
                    title: '数据库'
                }
            },
            {
                path: '/file',
                name: 'file',
                component: File,
                meta: {
                    title: '文件'
                }
            },
            {
                path: '/crontab',
                name: 'crontab',
                component: Crontab,
                meta: {
                    title: '计划任务'
                }
            },
            {
                path: '/soft',
                name: 'soft',
                component: Soft,
                meta: {
                    title: '软件商店'
                }
            },
            {
                path: '/setting',
                name: '/setting',
                component: Setting,
                meta: {
                    title: '面板设置'
                }
            },
            {
                name: 'notfound',
                path: '/404',
                meta: {
                    title: '404',
                },
                component: NotFound
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        meta: {
            title: '登录'
        },
        component: Login
    }
]

const router = new VueRouter({
    routes,
    mode: 'history'
})

router.beforeEach(function (to, from, next) {
    console.log(to)
    if (to.matched.length == 0) {
        next({
            path: '/404'
        })
        return
    }
    console.log(to.name)
    if (to.name != 'login') {
        let uuid = window.sessionStorage.getItem('uuid')
        if (uuid == null || !uuid) {
            next({
                path: '/login'
            })
            return
        }
    }
    next()
})

export default router